package com.guldan.jaina.task;

import com.guldan.core.enums.ProjectRegisterEn;
import com.guldan.jaina.dto.progress.BarDTO;
import com.guldan.jaina.dto.register.summary.SummaryDetailDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessSearchDTO;
import com.guldan.jaina.dto.users.UsersDTO;
import com.guldan.jaina.manager.RegisterSuccessManager;
import com.guldan.jaina.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by Silas.
 * Date: 2016/12/31
 * Time: 22:13
 */
public class BankReturnUploadThread implements Runnable {

    public Logger logger = LoggerFactory.getLogger(getClass());

    private RegisterSuccessManager registerSuccessManager;

    private LinkedBlockingQueue<SummaryDetailDTO> queue;

    private BarDTO barDTO;

    private String summaryId;

    private String threadName;

    private UsersDTO usersDTO;

    public void setRegisterSuccessManager(RegisterSuccessManager registerSuccessManager) {
        this.registerSuccessManager = registerSuccessManager;
    }

    public void setQueue(LinkedBlockingQueue<SummaryDetailDTO> queue) {
        this.queue = queue;
    }

    public void setBarDTO(BarDTO barDTO) {
        this.barDTO = barDTO;
    }

    public String getSummaryId() {
        return summaryId;
    }

    public void setSummaryId(String summaryId) {
        this.summaryId = summaryId;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public void setUsersDTO(UsersDTO usersDTO) {
        this.usersDTO = usersDTO;
    }

    @Override
    public void run() {
        //将当前操作用户加入线程变量
        UserUtils.setUser(this.usersDTO);

        logger.info(this.threadName + " begin");

        while (queue != null && queue.size() > 0) {
            try {

                Thread.sleep(1000L);

                SummaryDetailDTO dto = queue.take();

                RegisterSuccessSearchDTO searchDTO = new RegisterSuccessSearchDTO();
                searchDTO.setSummaryId(summaryId);
                searchDTO.setIdentNumber(dto.getIdentNumber());
                List<RegisterSuccessDTO> list = registerSuccessManager.getRegisterSuccessList(searchDTO);
                if (list != null) {
                    for (RegisterSuccessDTO registerSuccessDTO : list) {
                        registerSuccessDTO.setBankPayDate(dto.getBankPayDate());
                        registerSuccessDTO.setFailReason(dto.getFailReason());
                        if (dto.getIsSucceed()) {
                            this.barDTO.finishAdd(true);
                            registerSuccessDTO.setState(ProjectRegisterEn.bank_success.getCode());
                        } else {
                            this.barDTO.finishAdd(false);
                            registerSuccessDTO.setState(ProjectRegisterEn.bank_fail.getCode());
                        }
                        registerSuccessManager.save(registerSuccessDTO);
                    }
                } else {

                }
            } catch (Exception ex) {
                barDTO.finishAdd(false);
            } finally {
                logger.info("barDTO SSSSS " + barDTO.toString());
                //判断是否已经执行完成
                if (barDTO.getOver()) {
                    registerSuccessManager.bankReturnFinish(summaryId, barDTO);
                }

            }
        }
    }
}
